<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="类型" prop="">
        <el-select
          v-model="dataForm.type"
          placeholder="类型"
          clearable
          style="width: 200px"
        >
          <el-option label="hdzx" value="hdzx"></el-option>
          <el-option label="hdzh_gr" value="hdzh_gr"></el-option>
          <el-option label="hdzh_ds" value="hdzh_ds"></el-option>
          <el-option label="hdsx" value="hdsx"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="">
        <el-date-picker
          v-model="dataForm.createTime"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    // var validatePassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error("密码不能为空"));
    //   } else {
    //     callback();
    //   }
    // };
    // var validateComfirmPassword = (rule, value, callback) => {
    //   if (!this.dataForm.id && !/\S/.test(value)) {
    //     callback(new Error("确认密码不能为空"));
    //   } else if (this.dataForm.password !== value) {
    //     callback(new Error("确认密码与密码输入不一致"));
    //   } else {
    //     callback();
    //   }
    // };
    // var validateEmail = (rule, value, callback) => {
    //   if (!isEmail(value)) {
    //     callback(new Error("邮箱格式错误"));
    //   } else {
    //     callback();
    //   }
    // };
    // var validateMobile = (rule, value, callback) => {
    //   if (!isMobile(value)) {
    //     callback(new Error("手机号格式错误"));
    //   } else {
    //     callback();
    //   }
    // };
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id:0,
        type: "",
        createTime: "",

        roleIdList: [],
     
      },
      dataRule: {
        // userName: [
        //   { required: true, message: "用户名不能为空", trigger: "blur" },
        // ],
        // password: [{ validator: validatePassword, trigger: "blur" }],
        // comfirmPassword: [
        //   { validator: validateComfirmPassword, trigger: "blur" },
        // ],
        // email: [
        //   { required: true, message: "邮箱不能为空", trigger: "blur" },
        //   { validator: validateEmail, trigger: "blur" },
        // ],
        // mobile: [
        //   { required: true, message: "手机号不能为空", trigger: "blur" },
        //   { validator: validateMobile, trigger: "blur" },
        // ],
      },
    };
  },
  methods: {
    init(data) {
      // console.log(data.id,"ididididid");
      this.dataForm.id = data || 0;
      // console.log(this.dataForm.id);
      // console.log(this.dataForm.id,"this.dataForm.id");
      this.visible = true;
      if(this.dataForm.id===0){
        //增加
        console.log("增加");
        console.log(this.dataForm.id);
        this.dataForm.type="",
        this.dataForm.createTime=""
      }else{
        //修改
        console.log("修改");
        console.log(this.dataForm.id.id);
        this.dataForm = JSON.parse(JSON.stringify(data)); //深拷贝,拷贝的是对象
      //         this.$http({
      //   url: this.$http.adornUrl("/cpcppop/update"),
      //   method: "post",
      //   params: this.$http.adornParams({
      //     id:this.dataForm.id
      //   }),
      // }).then((res)=>{
      //   console.log(res,"resres");
      // })
      }
      // console.log(this.dataForm.id,"id");
      // this.$http({
      //   url: this.$http.adornUrl("/sys/role/select"),
      //   method: "get",
      //   params: this.$http.adornParams(),
      // }).then(({ data }) => {
      //     this.roleList = data && data.code === 0 ? data.list : [];
      //   }).then(() => {
      //     this.visible = true;
      //     this.$nextTick(() => {
      //       this.$refs["dataForm"].resetFields();
      //     });
      //   }).then(() => {
      //     if (this.dataForm.id) {
      //       console.log(this.dataForm.id,"this.dataForm.id");
      //       this.$http({
      //         url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
      //         method: "get",
      //         params: this.$http.adornParams(),
      //       }).then(({ data }) => {
      //          console.log(data,"dddddd");
      //         if (data && data.code === 0) {
      //           this.dataForm.userName = data.user.username;
      //           this.dataForm.salt = data.user.salt;
      //           this.dataForm.email = data.user.email;
      //           this.dataForm.mobile = data.user.mobile;
      //           this.dataForm.roleIdList = data.user.roleIdList;
      //           this.dataForm.status = data.user.status;
                
      //         }
      //       });
      //     }
      //   });
    },
    // 表单提交
    dataFormSubmit() {
      console.log(this.dataForm.id);
      // this.$refs["dataForm"].validate((valid) => {
        // if (valid) {
          console.log("ffffff");
          console.log(this.dataForm);
          // return
          this.$http({
            url: this.$http.adornUrl(
              `/cpcppop/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              // userId: this.dataForm.id || undefined,
              "type": this.dataForm.type,
              "createTime": this.dataForm.createTime,
             
            }),
          }).then(( res ) => {
            console.log(res);
            if (res.data && res.data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        // }
      // });
    },
  },
};
</script>
